Skip to content

[libc] Cleaned up wcsspn and wcscspn #147408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 22, 2025
Merged

Conversation

sribee8
Copy link
Contributor

@sribee8 sribee8 commented Jul 7, 2025

created internal wcsspn to avoid duplicated code

@llvmbot llvmbot added libc bazel "Peripheral" support tier build system: utils/bazel labels Jul 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 7, 2025

@llvm/pr-subscribers-libc

Author: None (sribee8)

Changes

created internal wcsspn to avoid duplicated code


Full diff: https://github.com/llvm/llvm-project/pull/147408.diff

5 Files Affected:

  • (modified) libc/src/wchar/CMakeLists.txt (+12)
  • (added) libc/src/wchar/wchar_utils.h (+33)
  • (modified) libc/src/wchar/wcscspn.cpp (+14-12)
  • (modified) libc/src/wchar/wcsspn.cpp (+15-13)
  • (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (+13)
diff --git a/libc/src/wchar/CMakeLists.txt b/libc/src/wchar/CMakeLists.txt
index f2f4b1d38f0f3..85d582d9ccc76 100644
--- a/libc/src/wchar/CMakeLists.txt
+++ b/libc/src/wchar/CMakeLists.txt
@@ -1,3 +1,13 @@
+add_header_library(
+  wchar_utils
+  HDRS
+    wchar_utils.h
+  DEPENDS
+    libc.hdr.types.size_t
+    libc.hdr.types.wchar_t
+    libc.src.__support.common
+)
+
 add_entrypoint_object(
   wcslen
   SRCS
@@ -211,6 +221,7 @@ add_entrypoint_object(
   DEPENDS
     libc.hdr.wchar_macros
     libc.hdr.types.size_t
+    libc.src.wchar.wchar_utils
 )
 
 add_entrypoint_object(
@@ -222,6 +233,7 @@ add_entrypoint_object(
   DEPENDS
     libc.hdr.wchar_macros
     libc.hdr.types.size_t
+    libc.src.wchar.wchar_utils
 )
 
 add_entrypoint_object(
diff --git a/libc/src/wchar/wchar_utils.h b/libc/src/wchar/wchar_utils.h
new file mode 100644
index 0000000000000..2fc3ac34469e7
--- /dev/null
+++ b/libc/src/wchar/wchar_utils.h
@@ -0,0 +1,33 @@
+//===-- wchar utils ---------------------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H
+#define LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H
+
+#include "hdr/types/size_t.h"
+#include "hdr/types/wchar_t.h"
+#include "src/__support/common.h"
+#include "src/__support/macros/attributes.h" // LIBC_INLINE
+
+namespace LIBC_NAMESPACE_DECL {
+namespace internal {
+
+template <typename Check>
+LIBC_INLINE size_t inline_wcsspn(const wchar_t *s1, Check check) {
+  size_t i = 0;
+  for (; s1[i]; ++i) {
+    if (!check(s1[i]))
+      return i;
+  }
+  return i;
+}
+
+} // namespace internal
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif //  LLVM_LIBC_SRC_WCHAR_WCHAR_UTILS_H
diff --git a/libc/src/wchar/wcscspn.cpp b/libc/src/wchar/wcscspn.cpp
index 8869d84cdfdee..afd3eb20e51ef 100644
--- a/libc/src/wchar/wcscspn.cpp
+++ b/libc/src/wchar/wcscspn.cpp
@@ -12,23 +12,25 @@
 #include "hdr/types/wchar_t.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "wchar_utils.h"
 
 namespace LIBC_NAMESPACE_DECL {
 
-bool check(wchar_t c, const wchar_t *s2) {
-  for (int n = 0; s2[n]; ++n) {
-    if (s2[n] == c)
-      return false;
+struct CheckCSpan {
+  const wchar_t *str;
+  CheckCSpan(const wchar_t *w) { str = w; }
+  bool operator()(wchar_t c) {
+    for (int n = 0; str[n]; ++n) {
+      if (str[n] == c)
+        return false;
+    }
+    return true;
   }
-  return true;
-}
+};
+
 LLVM_LIBC_FUNCTION(size_t, wcscspn, (const wchar_t *s1, const wchar_t *s2)) {
-  size_t i = 0;
-  for (; s1[i]; ++i) {
-    if (!check(s1[i], s2))
-      return i;
-  }
-  return i;
+  CheckCSpan check(s2);
+  return internal::inline_wcsspn(s1, check);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/wchar/wcsspn.cpp b/libc/src/wchar/wcsspn.cpp
index 23de381a2d954..f3c4be99f7e50 100644
--- a/libc/src/wchar/wcsspn.cpp
+++ b/libc/src/wchar/wcsspn.cpp
@@ -12,23 +12,25 @@
 #include "hdr/types/wchar_t.h"
 #include "src/__support/common.h"
 #include "src/__support/macros/config.h"
+#include "wchar_utils.h"
+
+struct CheckSpan {
+  const wchar_t *str;
+  CheckSpan(const wchar_t *w) { str = w; }
+  bool operator()(wchar_t c) {
+    for (int n = 0; str[n]; ++n) {
+      if (str[n] == c)
+        return true;
+    }
+    return false;
+  }
+};
 
 namespace LIBC_NAMESPACE_DECL {
 
-bool check(wchar_t c, const wchar_t *s2) {
-  for (int n = 0; s2[n]; ++n) {
-    if (s2[n] == c)
-      return true;
-  }
-  return false;
-}
 LLVM_LIBC_FUNCTION(size_t, wcsspn, (const wchar_t *s1, const wchar_t *s2)) {
-  size_t i = 0;
-  for (; s1[i]; ++i) {
-    if (!check(s1[i], s2))
-      return i;
-  }
-  return i;
+  CheckSpan check(s2);
+  return internal::inline_wcsspn(s1, check);
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index d3fc6912cd4e4..675263778b530 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -5729,6 +5729,17 @@ libc_function(
 
 ############################## wchar targets ###############################
 
+libc_support_library(
+    name = "wchar_utils",
+    hdrs = ["src/wchar/wchar_utils.h"],
+    deps = [
+        ":__support_common",
+        ":__support_macros_attributes",
+        ":types_size_t",
+        ":types_wchar_t",
+    ],
+)
+
 libc_function(
     name = "btowc",
     srcs = ["src/wchar/btowc.cpp"],
@@ -5826,6 +5837,7 @@ libc_function(
         ":__support_macros_config",
         ":types_size_t",
         ":types_wchar_t",
+        ":wchar_utils",
     ],
 )
 
@@ -5911,6 +5923,7 @@ libc_function(
         ":__support_macros_config",
         ":types_size_t",
         ":types_wchar_t",
+        ":wchar_utils",
     ],
 )
 

@sribee8 sribee8 merged commit 45d0750 into llvm:main Jul 22, 2025
14 of 29 checks passed
@sribee8 sribee8 deleted the wcsspn-cspn-fixes branch July 22, 2025 21:23
@llvm-ci
Copy link
Collaborator

llvm-ci commented Jul 22, 2025

LLVM Buildbot has detected a new failure on builder premerge-monolithic-linux running on premerge-linux-1 while building libc,utils at step 7 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/153/builds/38988

Here is the relevant piece of the build log for the reference
Step 7 (test-build-unified-tree-check-all) failure: test (failure)
...
PASS: lld :: COFF/arm64x-ctors-sec.s (98697 of 101798)
PASS: lld :: COFF/armnt-movt32t.test (98698 of 101798)
PASS: lld :: COFF/arm64x-includeoptional.s (98699 of 101798)
PASS: lld :: COFF/armnt-rel32.yaml (98700 of 101798)
PASS: lld :: COFF/associative-comdat-mingw.s (98701 of 101798)
PASS: lld :: COFF/arm64ec-import.test (98702 of 101798)
PASS: lld :: COFF/associative-comdat-mingw-weak.s (98703 of 101798)
PASS: lld :: COFF/arm64x-wrap.s (98704 of 101798)
PASS: lld :: COFF/arm64x-sameaddress.test (98705 of 101798)
TIMEOUT: MLIR :: Examples/standalone/test.toy (98706 of 101798)
******************** TEST 'MLIR :: Examples/standalone/test.toy' FAILED ********************
Exit Code: 1
Timeout: Reached timeout of 60 seconds

Command Output (stdout):
--
# RUN: at line 1
"/etc/cmake/bin/cmake" "/build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone" -G "Ninja"  -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang  -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir  -DLLVM_USE_LINKER=lld  -DPython3_EXECUTABLE="/usr/bin/python3.10"
# executed command: /etc/cmake/bin/cmake /build/buildbot/premerge-monolithic-linux/llvm-project/mlir/examples/standalone -G Ninja -DCMAKE_CXX_COMPILER=/usr/bin/clang++ -DCMAKE_C_COMPILER=/usr/bin/clang -DLLVM_ENABLE_LIBCXX=OFF -DMLIR_DIR=/build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir -DLLVM_USE_LINKER=lld -DPython3_EXECUTABLE=/usr/bin/python3.10
# .---command stdout------------
# | -- The CXX compiler identification is Clang 16.0.6
# | -- The C compiler identification is Clang 16.0.6
# | -- Detecting CXX compiler ABI info
# | -- Detecting CXX compiler ABI info - done
# | -- Check for working CXX compiler: /usr/bin/clang++ - skipped
# | -- Detecting CXX compile features
# | -- Detecting CXX compile features - done
# | -- Detecting C compiler ABI info
# | -- Detecting C compiler ABI info - done
# | -- Check for working C compiler: /usr/bin/clang - skipped
# | -- Detecting C compile features
# | -- Detecting C compile features - done
# | -- Looking for histedit.h
# | -- Looking for histedit.h - found
# | -- Found LibEdit: /usr/include (found version "2.11") 
# | -- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11") 
# | -- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.13") 
# | -- Using MLIRConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/mlir
# | -- Using LLVMConfig.cmake in: /build/buildbot/premerge-monolithic-linux/build/lib/cmake/llvm
# | -- Linker detection: unknown
# | -- Performing Test LLVM_LIBSTDCXX_MIN
# | -- Performing Test LLVM_LIBSTDCXX_MIN - Success
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR
# | -- Performing Test LLVM_LIBSTDCXX_SOFT_ERROR - Success
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER
# | -- Performing Test CXX_SUPPORTS_CUSTOM_LINKER - Success
# | -- Performing Test C_SUPPORTS_FPIC
# | -- Performing Test C_SUPPORTS_FPIC - Success
# | -- Performing Test CXX_SUPPORTS_FPIC

rupprecht added a commit to rupprecht/llvm-project that referenced this pull request Jul 23, 2025
rupprecht added a commit that referenced this pull request Jul 23, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bazel "Peripheral" support tier build system: utils/bazel libc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants